--[[
    数据缓存区
]]

local dateBuffer = BaseClass("dateBuffer")

function dateBuffer : __init(dateBuffers, goodsDate)
    -- 检查传入参数有效性
    if not dateBuffers or not goodsDate or not goodsDate.id then
        print("DateBuffer输出1:无效的缓存数据或商品数据")
        return
    end

    -- 查找是否已存在该物品
    local existingItem = nil
    for i = 1, #dateBuffers do
        if dateBuffers[i].id == goodsDate.id then
            existingItem = dateBuffers[i]
            break
        end
    end

    if existingItem then
        -- 已存在：更新数量+1
        existingItem.count = existingItem.count +  1
    else
        -- 不存在：创建新物品
        local newItem = {
            id = goodsDate.id,
            name = goodsDate.name,
            count =  1,
            price = goodsDate.price,
            icon = goodsDate.icon,
            description = goodsDate.description,
            type = goodsDate.type
        }
        table.insert(dateBuffers, newItem)
    end
end

-- 清除缓存方法
function dateBuffer.Clear(dateBuffers)
    if dateBuffers then
        while #dateBuffers > 0 do
            table.remove(dateBuffers)
        end
    end
end

return dateBuffer